/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.controlcenter;

import java.awt.FlowLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.kommando.core.controlcenter.PreferencePage;
import org.osgi.service.prefs.Preferences;

/**
 * @author Peter De Bruycker
 */
public class TestPreferencePage implements PreferencePage {

    private static final String TEST_KEY = "test";

    private JTextField textField;
    private JPanel panel;

    private Preferences prefs;

    public TestPreferencePage(Preferences prefs) {
        this.prefs = prefs;
    }

    @Override
    public boolean apply() {
        prefs.put(TEST_KEY, textField.getText());

        return true;
    }

    @Override
    public JComponent getComponent() {
        if (panel == null) {
            panel = new JPanel(new FlowLayout(FlowLayout.LEADING));

            textField = new JTextField(25);
            textField.setText(prefs.get(TEST_KEY, "default"));

            panel.add(textField);
        }

        return panel;
    }

    @Override
    public String getId() {
        return "test";
    }

    @Override
    public String getName() {
        return "Test";
    }

    @Override
    public String getParentId() {
        return "catalog";
    }

    @Override
    public void restoreDefaults() {
        textField.setText("default");
    }
}
